// 1.导入express对象
const express = require('express')
// 2.创建路由对象
const router = express.Router()
// 3.编写接口
const { getAll, getOne, exec } = require('../db')

/**
 * 1.添加用户
 */
router.post('/', async function (req, res, next) {
  const {
    user_username,
    user_password,
    user_type,
    use_truename,
    user_tel,
    user_onlinestatus,
    user_existstatus,
  } = req.body
  let sql = `insert into gg_user (user_username,user_password,user_type,use_truename,user_tel) values ('${user_username}','${user_password}','交通部门','${use_truename}','${user_tel}')`
  const data = await exec(sql)
  res.send({
    code: 0,
    message: '添加用户成功',
    results: {
      use_id: data.insertedId,
      user_username,
      user_password,
      user_type,
      use_truename,
      user_tel,
      user_onlinestatus,
      user_existstatus,
    },
  })
})

/**
 * 2.修改用户
 */
router.put('/:use_id', async function (req, res, next) {
  const { use_id } = req.params
  const { user_onlinestatus } = req.body
  let sql = `update gg_user set user_onlinestatus='${user_onlinestatus}' where use_id = '${use_id}' `
  const data = await exec(sql)
  res.send({
    code: 0,
    message: '修改用户成功',
  })
})
/**
 * 根据用户名修改用户存在状态(实际是删除用户)
 *
 */
router.put('/user_username/:user_username', async function (req, res, next) {
  // console.log(req.params)
  const { user_username } = req.params
  const { user_existstatus } = req.body
  let sql = `update gg_user set user_existstatus='${user_existstatus}' where user_username = '${user_username}' `
  const data = await exec(sql)
  res.send({
    code: 0,
    message: '修改用户存在状态成功',
  })
})
/**
 * 根据用户名修改用户密码
 *
 */
router.put('/update_user_password/:user_username', async function (req, res, next) {
  // console.log(req.params)
  const { user_username } = req.params
  const { user_password } = req.body
  let sql = `update gg_user set user_password='${user_password}' where user_username = '${user_username}' `
  const data = await exec(sql)
  if (data.changedRows) {
    res.send({
      code: 0,
      message: '修改用户密码成功',
    })
  } else {
    res.send({
      code: 11,
      message: '修改用户密码失败!!!',
    })
  }
})

/**
 * 3.删除用户
 */
// router.delete('/:use_id', async function (req, res, next) {
//   const { use_id } = req.params
//   let sql = `delete from gg_user where use_id=${use_id}`
//   await exec(sql)
//   res.send({
//     code: 0,
//     message: '删除成功',
//     results: '',
//   })
// })

/**
 * 4.获取分页用户列表
 * GET /user
 */
router.get('/', async function (req, res, next) {
  const { page, size, keyword, select } = req.query
  const offset = (page - 1) * size
  // 2. 操作数据
  if (!keyword) {
    let sql = `select * from gg_user limit ${offset}, ${size}`
    const data = await getAll(sql)
    sql = `select count(*) as total from gg_user`
    const { total } = await getOne(sql)
    // 3. 返回结果
    // res.status(响应状态码)
    res.send({
      code: 0,
      message: '获取分页用户成功',
      result: {
        total,
        data,
      },
    })
  } else {
    let sql = `select * from gg_user where user_username like '%${keyword}%' `
    const data = await getAll(sql)
    sql = `select count(*) as total from gg_user`
    const { total } = await getOne(sql)
    res.send({
      code: 0,
      message: '搜索成功',
      result: {
        total,
        data,
      },
    })
  }
})

/**
 * 5.根据id获取单个用户
 */
router.get('/:use_id', async function (req, res, next) {
  // 获取请求参数
  const { use_id } = req.params
  // 编写sql语句
  let sql = `select * from gg_user where use_id = ${use_id}`
  // 执行sql语句，返回结果
  const data = await getOne(sql)
  res.send({
    code: 0,
    message: '获取当前用户成功',
    result: {
      data,
    },
  })
})
/**
 * 6.批量删除
 */
router.delete('/:use_id', async function (req, res, next) {
  const { use_id } = req.params
  let sql = `delete from gg_user where use_id in (${use_id})`
  await exec(sql)
  res.send({
    code: 0,
    message: '批量删除成功',
    results: '',
  })
})
// 4.导出路由对象
module.exports = router
